package GUI.studentGUI;

import java.awt.SystemColor;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import GUI.MainGUI;
import controllers.LecturerController;
import controllers.StudentController;
import entities.ComputerizedExam;
import entities.Question;
import entities.QuestionPerExam;

public class StudentExamReport extends JPanel implements ActionListener,
		ListSelectionListener {

	private static final long serialVersionUID = 1L;
	private StudentController studentCtrl;
	private TextArea txtComnt;
	private JButton btnMainMenu;
	private JButton btnExit;
	private JLabel lblCmnt;
	private JList lstExams;
	private DefaultListModel model;
	private TextArea txtExmPreview;
	private ArrayList<ComputerizedExam> examsLst;
	private HashMap<String, ArrayList<QuestionPerExam>> qstnPerExmLst;

	public StudentExamReport(StudentController studentCtrl) {

		super();
		this.studentCtrl = studentCtrl;
		initialize();

	}

	/**
	 * initializing the GUI
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Student exams: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);
		add(GETtxtComnt());
		add(GETlstExams());
		add(GETtxtTxmPreview());
		add(GETbtnExit());
		add(GETlblCmnt());
		add(GETbtnMainMenu());
	}

	private JList GETlstExams() {
		if (lstExams == null) {
			lstExams = new JList();
			model = new DefaultListModel();
			lstExams.setModel(model);
			lstExams.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			lstExams.setBounds(10, 35, 115, 425);
		}
		return lstExams;
	}

	private TextArea GETtxtComnt() {
		if (txtComnt == null) {
			txtComnt = new TextArea();
			txtComnt.setEditable(false);
			txtComnt.setBounds(131, 315, 359, 145);
			txtComnt.setText("");
		}
		return txtComnt;
	}

	private TextArea GETtxtTxmPreview() {
		if (txtExmPreview == null) {
			txtExmPreview = new TextArea();
			txtExmPreview.setEditable(false);
			txtExmPreview.setBounds(131, 35, 359, 251);
		}
		return txtExmPreview;
	}

	private JButton GETbtnExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setBounds(401, 466, 89, 23);
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	private JLabel GETlblCmnt() {
		if (lblCmnt == null) {
			lblCmnt = new JLabel("Comments:");
			lblCmnt.setBounds(131, 295, 74, 14);
		}
		return lblCmnt;
	}

	private JButton GETbtnMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("Main Menu");
			btnMainMenu.setBounds(262, 466, 129, 23);
			btnMainMenu.addActionListener(this);
		}
		return btnMainMenu;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnMainMenu) {
			studentCtrl.handleStudentGUI(StudentController.MAIN);
		}
		if (e.getSource() == btnExit) {
			restoreToDefualts();
			studentCtrl.handleStudentGUI(LecturerController.EXIT);
		}

	}

	/**
	 * restore filed To Defaults
	 */
	public void restoreToDefualts() {
		model.removeAllElements();
		txtComnt.setEnabled(false);
		txtComnt.setText("");
	}
/**
 * add exam to combo
 * @param compExamsList
 */
	public void addExams(ArrayList<ComputerizedExam> compExamsList) {
		this.examsLst = new ArrayList<ComputerizedExam>();
		this.examsLst.addAll(compExamsList);
		model.removeAllElements();
		for (int i = 0; i < (compExamsList.size()); i++) {
			int size = lstExams.getModel().getSize();
			model.add(size, examsLst.get(i).getExamCode() + " : "
					+ examsLst.get(i).getExamGrade());

		}
		lstExams.addListSelectionListener(this);
	}
/**
 *
 * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
 */
	public void valueChanged(ListSelectionEvent e) {
		txtComnt.setEnabled(true);
		txtExmPreview.setText("");
		txtComnt.setText("");

		printExamPreview(lstExams.getSelectedIndex());
	}
/**
 * print the exam
 * @param selectedIndex
 */
	private void printExamPreview(int selectedIndex) {
		if (selectedIndex >= 0) {
			ComputerizedExam exm = examsLst.get(selectedIndex);
			ArrayList<QuestionPerExam> questionsLst = qstnPerExmLst.get(exm
					.getExamCode());
			txtExmPreview
					.append("Exam: "
							+ exm.getExamCode()
							+ " \texecuted by: "
							+ exm.getStudentID()
							+ "\n"
							+ "Started at: "
							+ new Time(exm.getExamBeginingHour().getTime())
									.toString()
							+ " and Ended at: "
							+ new Time(exm.getExamEndingHour().getTime())
									.toString()
							+ "\n"
							+ "by "
							+ (exm.getExamFinishStatus() == ComputerizedExam.STATUS_STUDENT_FINISHED ? "Student"
									: "Time's up.")
							+ "\n"
							+ (exm.getExamSuspectedCopy() > 0 ? "Automated exams check suspect this exam copied from "
									+ exm.getExamSuspectedCopy() + "'s exam!\n"
									: "\n") + "QUESTIONS:\n");
			for (int i = 0; i < questionsLst.size(); i++) {
				QuestionPerExam attendedQpE = questionsLst.get(i);
				Question attendedQ = questionsLst.get(i).getQuestion();
				txtExmPreview.append("\n\n#" + (i + 1) + ") ("
						+ attendedQpE.getQuestionGrade() + "):\n"
						+ attendedQ.getqInstructions() + "\n"
						+ attendedQ.getqText() + "\n\nCorrect answer:\n"
						+ attendedQ.getCorrectAns() + "\n\nStudent answer:\n");
				switch (exm.getAnswer(i).getSelectedAnswer()) {
				case 1:
					txtExmPreview.append(attendedQ.getCorrectAns());
					break;
				case 2:
					txtExmPreview.append(attendedQ.getAnswerA());
					break;
				case 3:
					txtExmPreview.append(attendedQ.getAnswerB());
					break;
				case 4:
					txtExmPreview.append(attendedQ.getAnswerC());
					break;
				}
			}
			txtExmPreview.select(0, 0);
			if (exm.getExamApproved())
				txtComnt.append(exm.getExamComments());
			else
				txtComnt.append("Please notice that your exam still need to be approved by your lecturer...");
		}
	}
/**
 * enter the question to the exam
 * @param allQsforExams
 */
	public void addQsForExams(
			HashMap<String, ArrayList<QuestionPerExam>> allQsforExams) {
		if (qstnPerExmLst == null)
			this.qstnPerExmLst = new  HashMap<String, ArrayList<QuestionPerExam>>();
		if (allQsforExams != null)
			this.qstnPerExmLst.putAll(allQsforExams);

	}
}
